package com.example.lianguisystem.service.Impl;

import com.alibaba.fastjson.JSON;
import com.example.lianguisystem.mapperLianGui.CommonMapper;
import com.example.lianguisystem.service.CommonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Service
public class CommonServiceImpl implements CommonService {

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private CommonMapper commonMapper;

    @Override
    public List<String> getMachineArr() {
        String str = stringRedisTemplate.opsForValue().get("salesOrder:machine");
        List<String> list = JSON.parseObject(str, List.class);
        return list;
    }

    @Override
    public String getBillCode(String pre, String tableName) {
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-");
        DateTimeFormatter tempDtf = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String format = LocalDate.now().format(tempDtf);
        pre = pre + LocalDate.now().format(dtf);
        Integer maxSeq = commonMapper.selectMaxSeq(pre, tableName);
        String billcode = pre + "00001";
        if (maxSeq != null) {
            billcode = pre + String.format("%05d", maxSeq + 1);
        }
        return billcode;
    }
}
